//
// Created by l on 19-2-20.
//

#include "MyDahuaCam.h"
namespace hitcrt{

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
     int32_t MyDahuaCam::setGrabMode( bool bContious)
    {
        int32_t bRet;
        IAcquisitionControlPtr sptrAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptrAcquisitionControl)  //采集控制失败
        {
            return -1;
        }

        CEnumNode enumNode = sptrAcquisitionControl->triggerSelector();
        bRet = enumNode.setValueBySymbol("FrameStart"); //设置帧扫描触发，不适用与连续拍照
        if (false == bRet) //设置失败
        {
            printf("set TriggerSelector fail.\n");
            return -1;
        }

        if (true == bContious) //连续抓图
        {
            enumNode = sptrAcquisitionControl->triggerMode();
            bRet = enumNode.setValueBySymbol("Off"); //设置关闭触发，否则相机不能连续拍照
            if (false == bRet)
            {
                printf("set triggerMode fail.\n");
                return -1;
            }
        }
        else  //单张抓图
        {
            enumNode = sptrAcquisitionControl->triggerMode();
            bRet = enumNode.setValueBySymbol("On"); //开启设置触发
            if (false == bRet)
            {
                printf("set triggerMode fail.\n");
                return -1;
            }

            /* ÉèÖÃŽ¥·¢ÔŽÎªÈíŽ¥·¢£šÓ²Ž¥·¢ÎªLine1£© */
            enumNode = sptrAcquisitionControl->triggerSource();
            bRet = enumNode.setValueBySymbol("Software");  //设置触发源为软件触发
            if (false == bRet)
            {
                printf("set triggerSource fail.\n");
                return -1;
            }
        }
        return 0;
    }
    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   int32_t MyDahuaCam::getGrabMode( bool &bContious)
    {
        int32_t bRet;
        IAcquisitionControlPtr sptrAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptrAcquisitionControl)
        {
            return -1;
        }

        CEnumNode enumNode = sptrAcquisitionControl->triggerSelector();
        bRet = enumNode.setValueBySymbol("FrameStart");
        if (false == bRet)
        {
            printf("set TriggerSelector fail.\n");
            return -1;
        }

        CString strValue;
        enumNode = sptrAcquisitionControl->triggerMode();
        bRet = enumNode.getValueSymbol(strValue);
        if (false == bRet)
        {
            printf("get triggerMode fail.\n");
            return -1;
        }

        if (strValue == "Off")
        {
            bContious = true;
        }
        else if (strValue == "On")
        {
            bContious = false;
        }
        else
        {
            printf("get triggerMode fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::triggerSoftware() //进行软件触发。
    {
        int32_t bRet;
        IAcquisitionControlPtr sptrAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr); //设置图像采集控制
        if (NULL == sptrAcquisitionControl)
        {
            return -1;
        }

        CCmdNode cmdNode = sptrAcquisitionControl->triggerSoftware();
        bRet = cmdNode.execute();
        if (false == bRet)
        {
            printf("triggerSoftware execute fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

    int32_t MyDahuaCam::setResolution(int nWidth, int nHeight)
    {
        // std::cout<<"h,w:"<<nHeight<<" "<<nWidth<<std::endl;
        this->width=nWidth;
        this->height=nHeight;
        int32_t bRet;
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
        if (NULL == sptrImageFormatControl)
        {
            return -1;
        }
        CIntNode intNode = sptrImageFormatControl->width();
        CIntNode intNode1=sptrImageFormatControl->offsetX();
        int64 inc=1;
        intNode.getIncrement(inc);
        int64 maxval,minval;
        intNode.getMaxVal(maxval);
        intNode.getMinVal(minval);
        nWidth=((nWidth-minval)/inc)*inc+minval;
        bRet = intNode.setValue(nWidth);
        if (false == bRet)
        {
            printf("set width fail.\n");
            // return -1;
        }
        int offsetx=(maxval-nWidth)/2.;
        intNode1.getMinVal(minval);
        intNode1.getMaxVal(maxval);
        intNode1.getIncrement(inc);
        offsetx=((offsetx-minval)/inc)*inc+minval;
        if(offsetx<minval){
            offsetx=minval;
        }else if(offsetx>maxval){
            offsetx=maxval;
        }
        bRet=intNode1.setValue(offsetx);
        if (false == bRet)
        {
            printf("set offsetx fail.\n");
            // return -1;
        }

        intNode = sptrImageFormatControl->height();
        intNode1=sptrImageFormatControl->offsetY();
        intNode.getMaxVal(maxval);
        intNode.getMinVal(minval);
        intNode.getIncrement(inc);
        nHeight=((nHeight-minval)/inc)*inc+minval;
        bRet = intNode.setValue(nHeight);
        if (false == bRet)
        {
            printf("set height fail.\n");
            // return -1;
        }
        int offsety=(maxval-nHeight)/2.;
        intNode1.getMinVal(minval);
        intNode1.getMaxVal(maxval);
        intNode1.getIncrement(inc);
        offsety=((offsety-minval)/inc)*inc+minval;
        if(offsety<minval){
            offsety=minval;
        }else if(offsety>maxval){
            offsety=maxval;
        }
        bRet=intNode1.setValue(offsety);
        if (false == bRet)
        {
            printf("set offsety fail.\n");
            // return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::getResolution( int64_t &nWidth, int64_t &nHeight)
    {
        int32_t bRet;
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
        if (NULL == sptrImageFormatControl)
        {
            return -1;
        }

        CIntNode intNode = sptrImageFormatControl->width();
        bRet = intNode.getValue(nWidth);
        if (false == bRet)
        {
            printf("get width fail.\n");
            return -1;
        }

        intNode = sptrImageFormatControl->height();
        bRet = intNode.getValue(nHeight);
        if (false == bRet)
        {
            printf("get height fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::setBinning()
    {
        CEnumNodePtr ptrParam(new CEnumNode(cameraSptr, "Binning"));
        if (ptrParam)
        {
            if (false == ptrParam->isReadable())
            {
                printf("binning not support.\n");
                return -1;
            }

            if (false == ptrParam->setValueBySymbol("XY"))
            {
                printf("set Binning XY fail.\n");
                return -1;
            }

            if (false == ptrParam->setValueBySymbol("Off"))
            {
                printf("set Binning Off fail.\n");
                return -1;
            }
        }
        return 0;
    }
    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

    int32_t MyDahuaCam::getMaxResolution(int64_t &nWidthMax, int64_t &nHeightMax)
    {
        /* width */
        {
            CIntNodePtr ptrParam(new CIntNode(cameraSptr, "SensorWidth"));
            if (ptrParam)
            {
                if (false == ptrParam->getValue(nWidthMax))
                {
                    printf("get WidthMax fail.\n");
                    return -1;
                }
            }
        }

        /* height */
        {
            CIntNodePtr ptrParam(new CIntNode(cameraSptr, "SensorHeight"));
            if (ptrParam)
            {
                if (false == ptrParam->getValue(nWidthMax))
                {
                    printf("get WidthMax fail.\n");
                    return -1;
                }
            }
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
     int32_t MyDahuaCam::setROI(int64_t nX, int64_t nY, int64_t nWidth, int64_t nHeight)
    {
        bool bRet;
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
        if (NULL == sptrImageFormatControl)
        {
            return -1;
        }

        /* width */
        CIntNode intNode = sptrImageFormatControl->width();
        bRet = intNode.setValue(nWidth);
        if (!bRet)
        {
            printf("set width fail.\n");
            return -1;
        }

        /* height */
        intNode = sptrImageFormatControl->height();
        bRet = intNode.setValue(nHeight);
        if (!bRet)
        {
            printf("set height fail.\n");
            return -1;
        }

        /* OffsetX */
        intNode = sptrImageFormatControl->offsetX();
        bRet = intNode.setValue(nX);
        if (!bRet)
        {
            printf("set offsetX fail.\n");
            return -1;
        }

        /* OffsetY */
        intNode = sptrImageFormatControl->offsetY();
        bRet = intNode.setValue(nY);
        if (!bRet)
        {
            printf("set offsetY fail.\n");
            return -1;
        }

        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
     int32_t MyDahuaCam::getROI( int64_t &nX, int64_t &nY, int64_t &nWidth, int64_t &nHeight)
    {
        bool bRet;
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
        if (NULL == sptrImageFormatControl)
        {
            return -1;
        }

        /* width */
        CIntNode intNode = sptrImageFormatControl->width();
        bRet = intNode.getValue(nWidth);
        if (!bRet)
        {
            printf("get width fail.\n");
        }

        /* height */
        intNode = sptrImageFormatControl->height();
        bRet = intNode.getValue(nHeight);
        if (!bRet)
        {
            printf("get height fail.\n");
        }

        /* OffsetX */
        intNode = sptrImageFormatControl->offsetX();
        bRet = intNode.getValue(nX);
        if (!bRet)
        {
            printf("get offsetX fail.\n");
        }

        /* OffsetY */
        intNode = sptrImageFormatControl->offsetY();
        bRet = intNode.getValue(nY);
        if (!bRet)
        {
            printf("get offsetY fail.\n");
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int32_t  MyDahuaCam::getWidth( int64_t &nWidth)
    {
        bool bRet;
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
        if (NULL == sptrImageFormatControl)
        {
            return -1;
        }

        CIntNode intNode = sptrImageFormatControl->width();
        bRet = intNode.getValue(nWidth);
        if (!bRet)
        {
            printf("get width fail.\n");
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
     int32_t MyDahuaCam::getHeight( int64_t &nHeight)
    {
        bool bRet;
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
        if (NULL == sptrImageFormatControl)
        {
            return -1;
        }

        CIntNode intNode = sptrImageFormatControl->height();
        bRet = intNode.getValue(nHeight);
        if (!bRet)
        {
            printf("get height fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t  MyDahuaCam::setExposureTime( double dExposureTime)
    {
        this->exposure=dExposureTime;
        bool bRet;
        IAcquisitionControlPtr sptrAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptrAcquisitionControl)
        {
            return -1;
        }
        bool bAutoExposure=false;
        if(dExposureTime>1e6||dExposureTime<1){
            bAutoExposure=true;
        }
        if (bAutoExposure)
        {
            std::cout<<"set auto exposure!"<<std::endl;
            CEnumNode enumNode = sptrAcquisitionControl->exposureAuto();
            bRet = enumNode.setValueBySymbol("Continuous");
            if (false == bRet)
            {
                printf("set exposureAuto fail.\n");
                return -1;
            }
        }
        else
        {
            CEnumNode enumNode = sptrAcquisitionControl->exposureAuto();
            bRet = enumNode.setValueBySymbol("Off");
            if (false == bRet)
            {
                printf("set exposureAuto fail.\n");
                return -1;
            }

            CDoubleNode doubleNode = sptrAcquisitionControl->exposureTime();
            bRet = doubleNode.setValue(dExposureTime);
            if (false == bRet)
            {
                printf("set exposureTime fail.\n");
                return -1;
            }
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   int32_t MyDahuaCam::getExposureTime( double &dExposureTime)
    {
        bool bRet;
        IAcquisitionControlPtr sptrAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptrAcquisitionControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAcquisitionControl->exposureTime();
        bRet = doubleNode.getValue(dExposureTime);
        if (false == bRet)
        {
            printf("get exposureTime fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

     int32_t MyDahuaCam::getExposureTimeMinMaxValue( double &dMinValue, double &dMaxValue)
    {
        bool bRet;
        IAcquisitionControlPtr sptrAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptrAcquisitionControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAcquisitionControl->exposureTime();
        bRet = doubleNode.getMinVal(dMinValue);
        if (false == bRet)
        {
            printf("get exposureTime minValue fail.\n");
            return -1;
        }

        bRet = doubleNode.getMaxVal(dMaxValue);
        if (false == bRet)
        {
            printf("get exposureTime maxValue fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   int32_t  MyDahuaCam::setGainRaw( double dGainRaw)
    {
        this->gainraw=dGainRaw;
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->gainRaw();
        bRet = doubleNode.setValue(dGainRaw);
        if (false == bRet)
        {
            printf("set gainRaw fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::getGainRaw( double &dGainRaw)
    {
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->gainRaw();
        bRet = doubleNode.getValue(dGainRaw);
        if (false == bRet)
        {
            printf("get gainRaw fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int32_t MyDahuaCam::getGainRawMinMaxValue( double &dMinValue, double &dMaxValue)
    {
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->gainRaw();
        bRet = doubleNode.getMinVal(dMinValue);
        if (false == bRet)
        {
            printf("get gainRaw minValue fail.\n");
            return -1;
        }

        bRet = doubleNode.getMaxVal(dMaxValue);
        if (false == bRet)
        {
            printf("get gainRaw maxValue fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int32_t MyDahuaCam::setGamma( double dGamma)
    {
        this->gamma=dGamma;
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->gamma();
        bRet = doubleNode.setValue(dGamma);
        if (false == bRet)
        {
            printf("set gamma fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::getGamma(double &dGamma)
    {
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->gamma();
        bRet = doubleNode.getValue(dGamma);
        if (false == bRet)
        {
            printf("get gamma fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::getGammaMinMaxValue( double &dMinValue, double &dMaxValue)
    {
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->gamma();
        bRet = doubleNode.getMinVal(dMinValue);
        if (false == bRet)
        {
            printf("get gamma minValue fail.\n");
            return -1;
        }

        bRet = doubleNode.getMaxVal(dMaxValue);
        if (false == bRet)
        {
            printf("get gamma maxValue fail.\n");
            return -1;
        }
        return 0;
    }

    int32_t MyDahuaCam::setBalanceRatioR(double dRedBalanceRatio){
        double r,g,b;
        if(getBalanceRatio(r,g,b)==0){
            setBalanceRatio(dRedBalanceRatio,g,b);
            return 0;
        }else{
            std::cout<<"get balance ratio faild"<<std::endl;
        }
        return -1;
    }
    int32_t MyDahuaCam::setBalanceRatioG(double dGreenBalanceRatio){
        double r,g,b;
        if(getBalanceRatio(r,g,b)==0){
            setBalanceRatio(r,dGreenBalanceRatio,b);
            return 0;
        }else{
            std::cout<<"get balance ratio faild"<<std::endl;
        }
        return -1;
    }
    int32_t MyDahuaCam::setBalanceRatioB(double dBlueBalanceRatio){
        double r,g,b;
        if(getBalanceRatio(r,g,b)==0){
            setBalanceRatio(r,g,dBlueBalanceRatio);
            return 0;
        } else{
            std::cout<<"get balance ratio faild"<<std::endl;
        }
        return -1;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int32_t MyDahuaCam::setBalanceRatio( double dRedBalanceRatio, double dGreenBalanceRatio, double dBlueBalanceRatio)
    {
        this->balance[0]=dRedBalanceRatio;
        this->balance[1]=dGreenBalanceRatio;
        this->balance[2]=dBlueBalanceRatio;
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        /* ¹Ø±Õ×Ô¶¯°×Æœºâ */
        CEnumNode enumNode = sptrAnalogControl->balanceWhiteAuto();
        if (false == enumNode.isReadable())
        {
            printf("balanceRatio not support.\n");
            return -1;
        }

        bRet = enumNode.setValueBySymbol("Off");
        if (false == bRet)
        {
            printf("set balanceWhiteAuto Off fail.\n");
            return -1;
        }

        enumNode = sptrAnalogControl->balanceRatioSelector();
        bRet = enumNode.setValueBySymbol("Red");
        if (false == bRet)
        {
            printf("set red balanceRatioSelector fail.\n");
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->balanceRatio();
        bRet = doubleNode.setValue(dRedBalanceRatio);
        if (false == bRet)
        {
            printf("set red balanceRatio fail.\n");
            return -1;
        }

        enumNode = sptrAnalogControl->balanceRatioSelector();
        bRet = enumNode.setValueBySymbol("Green");
        if (false == bRet)
        {
            printf("set green balanceRatioSelector fail.\n");
            return -1;
        }

        doubleNode = sptrAnalogControl->balanceRatio();
        bRet = doubleNode.setValue(dGreenBalanceRatio);
        if (false == bRet)
        {
            printf("set green balanceRatio fail.\n");
            return -1;
        }

        enumNode = sptrAnalogControl->balanceRatioSelector();
        bRet = enumNode.setValueBySymbol("Blue");
        if (false == bRet)
        {
            printf("set blue balanceRatioSelector fail.\n");
            return -1;
        }

        doubleNode = sptrAnalogControl->balanceRatio();
        bRet = doubleNode.setValue(dBlueBalanceRatio);
        if (false == bRet)
        {
            printf("set blue balanceRatio fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
int32_t MyDahuaCam::getBalanceRatio( double &dRedBalanceRatio, double &dGreenBalanceRatio, double &dBlueBalanceRatio)
    {
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CEnumNode enumNode = sptrAnalogControl->balanceRatioSelector();
        if (false == enumNode.isReadable())
        {
            printf("balanceRatio not support.\n");
            return -1;
        }

        bRet = enumNode.setValueBySymbol("Red");
        if (false == bRet)
        {
            printf("set red balanceRatioSelector fail.\n");
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->balanceRatio();
        bRet = doubleNode.getValue(dRedBalanceRatio);
        if (false == bRet)
        {
            printf("get red balanceRatio fail.\n");
            return -1;
        }

        enumNode = sptrAnalogControl->balanceRatioSelector();
        bRet = enumNode.setValueBySymbol("Green");
        if (false == bRet)
        {
            printf("set green balanceRatioSelector fail.\n");
            return -1;
        }

        doubleNode = sptrAnalogControl->balanceRatio();
        bRet = doubleNode.getValue(dGreenBalanceRatio);
        if (false == bRet)
        {
            printf("get green balanceRatio fail.\n");
            return -1;
        }

        enumNode = sptrAnalogControl->balanceRatioSelector();
        bRet = enumNode.setValueBySymbol("Blue");
        if (false == bRet)
        {
            printf("set blue balanceRatioSelector fail.\n");
            return -1;
        }

        doubleNode = sptrAnalogControl->balanceRatio();
        bRet = doubleNode.getValue(dBlueBalanceRatio);
        if (false == bRet)
        {
            printf("get blue balanceRatio fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::getBalanceRatioMinMaxValue( double &dMinValue, double &dMaxValue)
    {
        bool bRet;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptrAnalogControl->balanceRatio();
        if (false == doubleNode.isReadable())
        {
            printf("balanceRatio not support.\n");
            return -1;
        }

        bRet = doubleNode.getMinVal(dMinValue);
        if (false == bRet)
        {
            printf("get balanceRatio min value fail.\n");
            return -1;
        }

        bRet = doubleNode.getMaxVal(dMaxValue);
        if (false == bRet)
        {
            printf("get balanceRatio max value fail.\n");
            return -1;
        }

        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int32_t MyDahuaCam::setAcquisitionFrameRate(double dFrameRate)
    {
        bool bRet;
        IAcquisitionControlPtr sptAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptAcquisitionControl)
        {
            return -1;
        }

        CBoolNode booleanNode = sptAcquisitionControl->acquisitionFrameRateEnable();
        bRet = booleanNode.setValue(true);
        if (false == bRet)
        {
            printf("set acquisitionFrameRateEnable fail.\n");
            return -1;
        }

        CDoubleNode doubleNode = sptAcquisitionControl->acquisitionFrameRate();
        bRet = doubleNode.setValue(dFrameRate);
        if (false == bRet)
        {
            printf("set acquisitionFrameRate fail.\n");
            return -1;
        }
        return 0;
    }


    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int32_t MyDahuaCam::getAcquisitionFrameRate( double &dFrameRate)
    {
        bool bRet;
        IAcquisitionControlPtr sptAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptAcquisitionControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptAcquisitionControl->acquisitionFrameRate();
        bRet = doubleNode.getValue(dFrameRate);
        if (false == bRet)
        {
            printf("get acquisitionFrameRate fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

  int32_t MyDahuaCam::userSetSave()
    {
        bool bRet;
        IUserSetControlPtr sptUserSetControl = CSystem::getInstance().createUserSetControl(cameraSptr);
        if (NULL == sptUserSetControl)
        {
            return -1;
        }

        bRet = sptUserSetControl->saveUserSet(IUserSetControl::userSet1);
        if (false == bRet)
        {
            printf("saveUserSet fail.\n");
            return -1;
        }
        return 0;
    }


    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::loadUserSet()
    {
        bool bRet;
        IUserSetControlPtr sptUserSetControl = CSystem::getInstance().createUserSetControl(cameraSptr);
        if (NULL == sptUserSetControl)
        {
            return -1;
        }

        bRet = sptUserSetControl->setCurrentUserSet(IUserSetControl::userSet1);
        if (false == bRet)
        {
            printf("saveUserSet fail.\n");
            return -1;
        }
        return 0;
    }


    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
  int32_t MyDahuaCam::setTriggerDelay( double dDelayTime)
    {
        bool bRet;
        IAcquisitionControlPtr sptAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptAcquisitionControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptAcquisitionControl->triggerDelay();
        bRet = doubleNode.setValue(dDelayTime);
        if (false == bRet)
        {
            printf("set triggerDelay fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

    int32_t MyDahuaCam::getTriggerDelay( double &dDelayTime)
    {
        bool bRet;
        IAcquisitionControlPtr sptAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptAcquisitionControl)
        {
            return -1;
        }

        CDoubleNode doubleNode = sptAcquisitionControl->triggerDelay();
        bRet = doubleNode.getValue(dDelayTime);
        if (false == bRet)
        {
            printf("set triggerDelay fail.\n");
            return -1;
        }
        return 0;
    }
    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

  int32_t  MyDahuaCam::setLineTriggerMode( bool bRisingEdge)
    {
        bool bRet;
        IAcquisitionControlPtr sptAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptAcquisitionControl)
        {
            return -1;
        }

        CEnumNode enumNode = sptAcquisitionControl->triggerSelector();
        //第一步：设置触发选择为单帧触发
        if (false == enumNode.setValueBySymbol("FrameStart"))
        {
            printf("set triggerSelector fail.\n");
            return -1;
        }
        //第二步：设置触发模式为开
        enumNode = sptAcquisitionControl->triggerMode();
        if (false == enumNode.setValueBySymbol("On"))
        {
            printf("set triggerMode fail.\n");
            return -1;
        }
        //第三步：设置触发源为硬件线1触发
        enumNode = sptAcquisitionControl->triggerSource();
        if (false == enumNode.setValueBySymbol("Line1"))
        {
            printf("set triggerSource fail.\n");
            return -1;
        }
        //第四步：设置触发方式为沿触发：1为上升沿，0为下降沿
        enumNode = sptAcquisitionControl->triggerActivation();
        if (true == bRisingEdge)
        {
            bRet = enumNode.setValueBySymbol("RisingEdge");
        }
        else
        {
            bRet = enumNode.setValueBySymbol("FallingEdge");
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   int32_t MyDahuaCam::getLineTriggerMode( bool &bRisingEdge)
    {
        bool bRet;
        IAcquisitionControlPtr sptAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
        if (NULL == sptAcquisitionControl)
        {
            return -1;
        }

        CEnumNode enumNode = sptAcquisitionControl->triggerSelector();
        if (false == enumNode.setValueBySymbol("FrameStart"))
        {
            printf("set triggerSelector fail.\n");
            return -1;
        }

        CString strValue;
        enumNode = sptAcquisitionControl->triggerActivation();
        if (true == bRisingEdge)
        {
            bRet = enumNode.getValueSymbol(strValue);
        }
        else
        {
            bRet = enumNode.getValueSymbol(strValue);
        }

        if (false == bRet)
        {
            printf("get triggerActivation fail.\n");
            return -1;
        }

        if (strValue == "RisingEdge")
        {
            bRisingEdge = true;
        }
        else if (strValue == "FallingEdge")
        {
            bRisingEdge = false;
        }
        else
        {
            printf("get triggerActivation fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
  int32_t MyDahuaCam::setLineDebouncerTimeAbs(double dLineDebouncerTimeAbs)
    {
        IDigitalIOControlPtr sptDigitalIOControl = CSystem::getInstance().createDigitalIOControl(cameraSptr);
        if (NULL == sptDigitalIOControl)
        {
            return -1;
        }

        CEnumNode enumNode = sptDigitalIOControl->lineSelector();
        if (false == enumNode.setValueBySymbol("Line1"))
        {
            printf("set lineSelector fail.\n");
            return -1;
        }

        CDoubleNode doubleNode = sptDigitalIOControl->lineDebouncerTimeAbs();
        if (false == doubleNode.setValue(dLineDebouncerTimeAbs))
        {
            printf("set lineDebouncerTimeAbs fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int32_t MyDahuaCam::getLineDebouncerTimeAbs( double &dLineDebouncerTimeAbs)
    {
        IDigitalIOControlPtr sptDigitalIOControl = CSystem::getInstance().createDigitalIOControl(cameraSptr);
        if (NULL == sptDigitalIOControl)
        {
            return -1;
        }

        CEnumNode enumNode = sptDigitalIOControl->lineSelector();
        if (false == enumNode.setValueBySymbol("Line1"))
        {
            printf("set lineSelector fail.\n");
            return -1;
        }

        CDoubleNode doubleNode = sptDigitalIOControl->lineDebouncerTimeAbs();
        if (false == doubleNode.getValue(dLineDebouncerTimeAbs))
        {
            printf("get lineDebouncerTimeAbs fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   int32_t MyDahuaCam::setOutputTime( int nTimeMS)
    {
        IDigitalIOControlPtr sptDigitalIOControl = CSystem::getInstance().createDigitalIOControl(cameraSptr);
        if (NULL == sptDigitalIOControl)
        {
            return -1;
        }

        CEnumNode paramLineSource(cameraSptr, "LineSource");
        if (false == paramLineSource.setValueBySymbol("UserOutput1"))
        {
            printf("set LineSource fail.");
            return -1;
        }

        /* œ«Êä³öÐÅºÅÀ­žßÈ»ºóÀ­µÍ */
        CBoolNode booleanNode = sptDigitalIOControl->userOutputValue();
        if (false == booleanNode.setValue(true))
        {
            printf("set userOutputValue fail.\n");
            return -1;
        }

        CThread::sleep(nTimeMS);

        if (false == booleanNode.setValue(false))
        {
            printf("set userOutputValue fail.\n");
            return -1;
        }
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

 int32_t  MyDahuaCam::setReverseX( bool flag)
    {
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);

        CBoolNode boolNodeReverseX = sptrImageFormatControl->reverseX();
        if(!boolNodeReverseX.setValue(flag))
        {
            printf("set reverseX fail.\n");
            return -1;
        }

        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   int32_t MyDahuaCam::setReverseY( bool flag)
    {
        IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);

        CBoolNode boolNodeReverseY = sptrImageFormatControl->reverseY();
        if(!boolNodeReverseY.setValue(flag))
        {
            printf("set reverseY fail.\n");
            return -1;
        }

        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

    int32_t MyDahuaCam::autoSetCameraIP()
    {
        IGigECameraPtr gigeCameraPtr = IGigECamera::getInstance(cameraSptr);
        if(NULL == gigeCameraPtr)
        {
            return -1;
        }

        //»ñÈ¡GigeÏà»úÏà¹ØÐÅÏ¢
        CString ip = gigeCameraPtr->getIpAddress();
        CString subnetMask = gigeCameraPtr->getSubnetMask();
        CString gateway = gigeCameraPtr->getGateway();
        CString macAddress = gigeCameraPtr->getMacAddress();
        printf("ip address is %s.\r\n", ip.c_str());
        printf("subnetMask is %s.\r\n", subnetMask.c_str());
        printf("gateway is %s.\r\n", gateway.c_str());
        printf("macAddress is %s.\r\n", macAddress.c_str());
        printf("\n");

        unsigned long devIpValue = ntohl(inet_addr(gigeCameraPtr->getIpAddress().c_str()));
        unsigned long devSubMaskValue = ntohl(inet_addr(gigeCameraPtr->getSubnetMask().c_str()));

        //»ñÈ¡¶ÔÓŠœÓ¿ÚµÄÍø¿šÐÅÏ¢
        IGigEInterfacePtr gigeInterfaceSPtr = IGigEInterface::getInstance(cameraSptr);
        if(NULL == gigeInterfaceSPtr)
        {
            return -1;
        }

        CString interfaceIp = gigeInterfaceSPtr->getIpAddress();
        CString interfaceSubnetMask = gigeInterfaceSPtr->getSubnetMask();
        CString interfaceGateway = gigeInterfaceSPtr->getGateway();
        CString interfaceMacAddress = gigeInterfaceSPtr->getMacAddress();
        printf("ip address of interface  is %s.\r\n", interfaceIp.c_str());
        printf("subnetMask of interface is %s.\r\n", interfaceSubnetMask.c_str());
        printf("gateway of interface is %s.\r\n", interfaceGateway.c_str());
        printf("macAddress of interface is %s.\r\n", interfaceMacAddress.c_str());
        printf("\n");

        unsigned long InterfaceIpValue = ntohl(inet_addr(gigeInterfaceSPtr->getIpAddress().c_str()));
        unsigned long InterfaceSubMaskValue = ntohl(inet_addr(gigeInterfaceSPtr->getSubnetMask().c_str()));

        if( (devIpValue & devSubMaskValue) != (InterfaceIpValue & InterfaceSubMaskValue) )
        {
            //Éè±žÓëÍø¿š²»ÔÚÍ¬Ò»Íø¶Î£¬Ç¿ÖÆÉèÖÃÉè±žÓëÍø¿šÔÚÍ¬Ò»Íø¶Î
            unsigned char newIPStr[20] = {0};

            while(1)
            {
                unsigned long newIpValue = rand() % 254 + 1; //1~254
                if(newIpValue != (InterfaceIpValue & 0xff))
                {
                    newIpValue = (InterfaceIpValue & 0xffffff00) + newIpValue;
                    struct in_addr   stInAddr;
                    stInAddr.s_addr	= htonl(newIpValue);
                    memcpy(newIPStr, inet_ntoa(stInAddr), strlen(inet_ntoa(stInAddr)));
                    break;
                }
            }

            if(!gigeCameraPtr->forceIpAddress((const char*)newIPStr, gigeInterfaceSPtr->getSubnetMask().c_str(), gigeInterfaceSPtr->getGateway().c_str()))
            {
                printf("Set device ip failed.\n");
                return -1;
            }
        }

        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   int32_t MyDahuaCam::setCameraIp( char* ipAddress, char* subnetMask, char* gateway)
    {
        IGigECameraPtr gigeCameraPtr = IGigECamera::getInstance(cameraSptr);
        if(NULL == gigeCameraPtr)
        {
            return -1;
        }

        if(!gigeCameraPtr->forceIpAddress(ipAddress, subnetMask, gateway))
        {
            printf("Set device ip failed.\n");
            return -1;
        }

        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

    int32_t MyDahuaCam::setCameraPersistentIP()
    {
        IGigECameraPtr gigeCameraPtr = IGigECamera::getInstance(cameraSptr);
        if(NULL == gigeCameraPtr)
        {
            printf("gigeCameraPtr is null.\n");
            return -1;
        }

        ITransportLayerControlPtr transportLayerControlPtr= CSystem::getInstance().createTransportLayerControl(cameraSptr);

        if(NULL == transportLayerControlPtr)
        {
            printf("transportLayerControlPtr is null.\n");
            return -1;
        }

        transportLayerControlPtr->gevCurrentIPConfigurationPersistentIP().setValue(true);
        transportLayerControlPtr->gevPersistentDefaultGateway().setValue(gigeCameraPtr->getGateway().c_str());
        transportLayerControlPtr->gevPersistentIPAddress().setValue(gigeCameraPtr->getIpAddress().c_str());
        transportLayerControlPtr->gevPersistentSubnetMask().setValue(gigeCameraPtr->getSubnetMask().c_str());

        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 设置亮度。
* @param int _value:亮度值范围0-100
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

    int32_t MyDahuaCam::setBrightness(int _value){
        this->brightness=_value;
        IISPControlPtr iispControlPtr=CSystem::getInstance().createISPControl(cameraSptr);
        if(iispControlPtr==NULL){
            return -1;
        }
        CIntNode intNode=iispControlPtr->brightness();
        int64 vmax,vmin,vinc;
        intNode.getMaxVal(vmax);
        intNode.getMinVal(vmin);
        intNode.getIncrement(vinc);
        if(_value>vmax)
            _value=vmax;
        if(_value<vmin)
            _value=vmin;
        _value=((_value-vmin)/vinc)*vinc+vmin;
        if(!intNode.setValue(_value)){
            printf("set brightness fail.\n");
            return -1;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 不知道是什么感觉和亮度很像
* @param int _value:亮度值范围0或1
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::setDigitalShift(int _value){
        this->digitalshift=_value;
        IISPControlPtr iispControlPtr=CSystem::getInstance().createISPControl(cameraSptr);
        if(iispControlPtr==NULL){
            return -1;
        }
        CIntNode intNode=iispControlPtr->digitalshift();
        int64 vmin,vmax,vinc;
        intNode.getMinVal(vmin);
        intNode.getMaxVal(vmax);
        intNode.getIncrement(vinc);
        if(_value>vmax)
            _value=vmax;
        if(_value<=vmin)
            _value=vmin;
        _value=((_value-vmin)/vinc)*vinc+vmin;
        if(!intNode.setValue(_value)){
            printf("set digitalshift fail.\n");
            return -1;
        }
        return 0;
    }
    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief blacklevel
* @param int _value:取值范围0~255
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::setBlackLevel(int _value){
        this->blacklevel=_value;
        IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
        if (NULL == sptrAnalogControl)
        {
            return -1;
        }
        CIntNode intNode=sptrAnalogControl->blackLevel();
        CEnumNode enumNode=sptrAnalogControl->blackLevelAuto();
        int64 vmin,vmax,vinc;
        intNode.getMinVal(vmin);
        intNode.getMaxVal(vmax);
        intNode.getIncrement(vinc);
        if(_value<vmin){
            _value=vmin;
        } else if(_value>vmax){
            _value=vmax;
        }
        _value=((_value-vmin)/vinc)*vinc+vmin;
        if(!enumNode.setValue(0)){
            printf("set blacklevelauto fail.\n");
           // return -1;
        }
        if(!intNode.setValue(_value)){
            printf("set blacklevel fail.\n");
            return -1;
        }
        return 0;

    }
    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 设置锐度
* @param int _value:取值范围0~100
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    int32_t MyDahuaCam::setSharpness(int _value){
        this->sharpness=_value;
        IISPControlPtr iispControlPtr=CSystem::getInstance().createISPControl(cameraSptr);
        if(NULL==iispControlPtr){
            return -1;
        }
        CIntNode intNode=iispControlPtr->sharpness();
        CEnumNode enumNode=iispControlPtr->sharpnessEnable();
        int64 vmin,vmax,vinc;
        intNode.getMinVal(vmin);
        intNode.getMaxVal(vmax);
        intNode.getIncrement(vinc);
        if(_value<vmin){
            _value=vmin;
        } else if(_value>vmax){
            _value=vmax;
        }
        _value=((_value-vmin)/vinc)*vinc+vmin;
        if(!enumNode.setValue(1)){
            printf("set digitalshift fail.\n");
            return -1;
        }
        if(!intNode.setValue(_value)){
            printf("set digitalshift fail.\n");
            return -1;
        }

        return  0;

    }

int32_t MyDahuaCam::getHeightRange(int64& min,int64& max,int64& inc){
    if(!inited){
        return -1;
    }
    IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
    if (NULL == sptrImageFormatControl)
    {
        return -1;
    }
    CIntNode intNode = sptrImageFormatControl->height();
    intNode.getMinVal(min);
    intNode.getMaxVal(max);
    intNode.getIncrement(inc);
    return 0;
}
int32_t MyDahuaCam::getWidthRange(int64& min,int64& max,int64& inc){
    if(!inited){
        return -1;
    }
    IImageFormatControlPtr sptrImageFormatControl = CSystem::getInstance().createImageFormatControl(cameraSptr);
    if (NULL == sptrImageFormatControl)
    {
        return -1;
    }
    CIntNode intNode = sptrImageFormatControl->width();
    intNode.getMinVal(min);
    intNode.getMaxVal(max);
    intNode.getIncrement(inc);
    return 0;
}
int32_t MyDahuaCam::getExposureRange(double& min,double& max){
    if(!inited){
        return -1;
    }
    IAcquisitionControlPtr sptrAcquisitionControl = CSystem::getInstance().createAcquisitionControl(cameraSptr);
    if (NULL == sptrAcquisitionControl)
    {
        return -1;
    }
    CDoubleNode doubleNode = sptrAcquisitionControl->exposureTime();
    doubleNode.getMinVal(min);
    doubleNode.getMaxVal(max);
    return 0;
}
int32_t MyDahuaCam::getGammaRange(double& min,double& max){
    if(!inited){
        return -1;
    }
    IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
    if (NULL == sptrAnalogControl)
    {
        return -1;
    }

    CDoubleNode doubleNode = sptrAnalogControl->gamma();
    doubleNode.getMinVal(min);
    doubleNode.getMaxVal(max);
    return 0;
}
int32_t MyDahuaCam::getSharpnessRange(int64& min,int64& max,int64& inc){
    if(!inited){
        return -1;
    }
    IISPControlPtr iispControlPtr=CSystem::getInstance().createISPControl(cameraSptr);
    if(NULL==iispControlPtr){
        return -1;
    }
    CIntNode intNode=iispControlPtr->sharpness();
    intNode.getMinVal(min);
    intNode.getMaxVal(max);
    intNode.getIncrement(inc);
    return 0;
}
int32_t MyDahuaCam::getBrightnessRange(int64& min,int64& max,int64& inc){
    if(!inited){
        return -1;
    }
    IISPControlPtr iispControlPtr=CSystem::getInstance().createISPControl(cameraSptr);
    if(iispControlPtr==NULL){
        return -1;
    }
    CIntNode intNode=iispControlPtr->brightness();
    intNode.getMaxVal(max);
    intNode.getMinVal(min);
    intNode.getIncrement(inc);
    return 0;
}
int32_t MyDahuaCam::getBlacklevelRange(int64& min,int64& max,int64& inc){
    if(!inited){
        return -1;
    }
    IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
    if (NULL == sptrAnalogControl)
    {
        return -1;
    }
    CIntNode intNode=sptrAnalogControl->blackLevel();
    intNode.getMinVal(min);
    intNode.getMaxVal(max);
    intNode.getIncrement(inc);
    return 0;
}
int32_t MyDahuaCam::getDigitalshiftRange(int64& min,int64& max,int64& inc){
    if(!inited){
        return -1;
    }
    IISPControlPtr iispControlPtr=CSystem::getInstance().createISPControl(cameraSptr);
    if(iispControlPtr==NULL){
        return -1;
    }
    CIntNode intNode=iispControlPtr->digitalshift();
    intNode.getMaxVal(max);
    intNode.getMinVal(min);
    intNode.getIncrement(inc);
    return 0;
}
int32_t MyDahuaCam::getGainrawRange(double& min,double& max){
    if(!inited){
        return -1;
    }
    IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
    if (NULL == sptrAnalogControl)
    {
        return -1;
    }

    CDoubleNode doubleNode = sptrAnalogControl->gainRaw();
    doubleNode.getMinVal(min);
    doubleNode.getMaxVal(max);
    return 0;
}
int32_t MyDahuaCam::getBalanceRange(double& min,double& max){
    if(!inited){
        return -1;
    }
    IAnalogControlPtr sptrAnalogControl = CSystem::getInstance().createAnalogControl(cameraSptr);
    if (NULL == sptrAnalogControl)
    {
        return -1;
    }
    CDoubleNode doubleNode = sptrAnalogControl->balanceRatio();
    doubleNode.getMinVal(min);
    doubleNode.getMaxVal(max);
}



    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

   void MyDahuaCam::modifyCamralExposureTime(CSystem &systemObj)
    {
        IAcquisitionControlPtr sptrAcquisitionControl = systemObj.createAcquisitionControl(cameraSptr);
        if (NULL == sptrAcquisitionControl)
        {
            return;
        }

        double exposureTimeValue = 0.0;
        CDoubleNode exposureTime = sptrAcquisitionControl->exposureTime();

        exposureTime.getValue(exposureTimeValue);
        printf("before change ,exposureTime is %f. thread ID :%d\n", exposureTimeValue, CThread::getCurrentThreadID());

        exposureTime.setValue(exposureTimeValue + 2);
        exposureTime.getValue(exposureTimeValue);
        printf("after change ,exposureTime is %f. thread ID :%d\n", exposureTimeValue, CThread::getCurrentThreadID());
    }


    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/

    void MyDahuaCam::LogPrinterFunc(const char* log)
    {
        return;
    }

    bool checkCamera(TVector<ICameraPtr>& vCameraPtrList){
        TVector<ICameraPtr>::iterator it;
        for(it=vCameraPtrList.begin();it!=vCameraPtrList.end();){
            const char* vendorName = (*it)->getVendorName();
            std::string v_d(vendorName);
            if(v_d!="Dahua Technology"){
                vCameraPtrList.erase(it);
            } else{
                ++it;
            }
        }
        if(vCameraPtrList.empty()){
            return false;
        } else{
            return true;
        }
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 显示并通过序列号选择设备
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    void MyDahuaCam::displayDeviceInfo(TVector<ICameraPtr>& vCameraPtrList)
    {
        ICameraPtr cameraSptr;
        printf("\nIdx Type Vendor     Model      S/N             DeviceUserID    IP Address    \n");
        printf("------------------------------------------------------------------------------\n");
        for (int cameraIndex = 0; cameraIndex < vCameraPtrList.size(); cameraIndex++)
        {
            cameraSptr = vCameraPtrList[cameraIndex];

            const char* vendorName = cameraSptr->getVendorName();
            std::string v_d(vendorName);
            if(v_d!="Dahua Technology"){
                continue;
            }
            printf("%-3d", cameraIndex + 1);
            switch (cameraSptr->getType())
            {
                case ICamera::typeGige:
                    printf(" GigE");
                    break;
                case ICamera::typeU3v:
                    printf(" U3V ");
                    break;
                case ICamera::typeCL:
                    printf(" CL  ");
                    break;
                case ICamera::typePCIe:
                    printf(" PCIe");
                    break;
                default:
                    printf("     ");
                    break;
            }
            char vendorNameCat[11];
            if (strlen(vendorName) > 10)
            {
                strncpy(vendorNameCat, vendorName, 7);
                vendorNameCat[7] = '\0';
                strcat(vendorNameCat, "...");
                printf(" %-10.10s", vendorNameCat);
            }
            else
            {
                printf(" %-10.10s", vendorName);
            }

            printf(" %-10.10s", cameraSptr->getModelName());

            std::string s_num=cameraSptr->getSerialNumber();
            if("auto"==mSerialNumber){
                mcam_index=cameraIndex;
            }else if(s_num==mSerialNumber){
                mcam_index=cameraIndex;
            }
            printf(" %-15.15s", s_num.c_str());

            const char* deviceUserID = cameraSptr->getName();
            char deviceUserIDCat[16] = {0};
            if (strlen(deviceUserID) > 15)
            {
                strncpy(deviceUserIDCat, deviceUserID, 12);
                deviceUserIDCat[12] = '\0';
                strcat(deviceUserIDCat, "...");
                printf(" %-15.15s", deviceUserIDCat);
            }
            else
            {
                memcpy(deviceUserIDCat, deviceUserID, sizeof(deviceUserIDCat));
                printf(" %-15.15s", deviceUserIDCat);
            }

            IGigECameraPtr gigeCameraPtr = IGigECamera::getInstance(cameraSptr);
            if (NULL != gigeCameraPtr.get())
            {
                CString ip = gigeCameraPtr->getIpAddress();
                printf(" %s", ip.c_str());
            }
            printf("\n");

        }
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    char* MyDahuaCam::trim(char* pStr)
    {
        char* pDst = pStr;
        char* pTemStr = NULL;
        int ret = -1;

        if (pDst != NULL)
        {
            pTemStr = pDst + strlen(pStr) - 1;
            //³ýÈ¥×Ö·ûŽ®Ê×²¿¿Õžñ
            while ( *pDst ==' ')
            {
                pDst++;
            }
            //³ýÈ¥×Ö·ûŽ®Î²²¿¿Õžñ
            while ((pTemStr > pDst) &&(*pTemStr == ' '))
            {
                *pTemStr-- = '\0';
            }
        }
        return pDst;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
   int MyDahuaCam::isInputValid(char* pInpuStr)
    {
        char numChar;
        char* pStr = pInpuStr;
        while (*pStr != '\0')
        {
            numChar = *pStr;
            if ((numChar > '9') || (numChar < '0'))
            {
                return -1;
            }
            pStr++;
        }
        return 0;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 参数相关,原始sdk提供
* @param
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
  int MyDahuaCam::selectDevice(int cameraCnt)
    {
        char inputStr[256] = {0};
        char* pTrimStr;
        int inputIndex = -1;
        int ret = -1;
        /* ÌáÊŸÓÃ»§Ñ¡Ôñ */
        printf("\nPlease input the camera index: ");
        while (1)
        {
            /* »ñÈ¡ÊäÈëÄÚÈÝ×Ö·ûŽ® */
            memset(inputStr, 0, sizeof(inputStr));
            gets(inputStr);////如果显示gets有问题，将c++标准改成11或替换成getline
            // std::cin.getline(inputStr,sizeof(inputStr));
            /* Çå¿ÕÊäÈë»ºŽæ */
            fflush(stdin);

            /*³ýÈ¥×Ö·ûŽ®Ê×Î²¿Õžñ */
            pTrimStr = trim(inputStr);
            //ÅÐ¶ÏÊäÈë×Ö·ûŽ®ÊÇ·ñÎªÊý×Ö
            ret = isInputValid(pTrimStr);
            if (ret == 0)
            {
                /* ÊäÈëµÄ×Ö·ûŽ®×ª»»³ÉÎªÊý×Ö */
                inputIndex = atoi(pTrimStr);
                /* ÅÐ¶ÏÓÃ»§Ñ¡ÔñºÏ·šÐÔ */
                inputIndex -= 1;//ÏÔÊŸµÄIndexÊÇŽÓ1¿ªÊŒ
                if ((inputIndex >= 0) && (inputIndex < cameraCnt))
                {
                    break;
                }
            }

            printf("Input invalid! Please input the camera index: ");
        }
        return inputIndex;
    }

    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 构造函数
* @param std::string _SerialNumber：相机序列号,序列号可以在自带软件里面看到；
     * int _width：图像宽度,128～1280,网口相机为64-800;
     * int _height:图像高度64～1024,网口相机为32-600；
     * double _exposure：曝光,1~1e6
     * int _brightness:亮度0~100
     * int _digitalshift:类似亮度0~4
     * std::vector<double> _balance：白平衡0~15
     * int blacklevel:跟gama类似0~255
     * double gama：0~3.99998
     * int gainraw:增益1~32
     * int sharpness:0-100
* @return
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    MyDahuaCam::MyDahuaCam(std::string _SerialNumber,int _width,int _height,double _exposure,int _brightness,int _digitalshift,std::vector<double> _balance,int _blacklevel,double _gamma,int _gainraw,int _sharpness){
        this->mSerialNumber=_SerialNumber;
        this->width=_width;
        this->height=_height;
        this->exposure=_exposure;
        this->brightness=_brightness;
        this->digitalshift=_digitalshift;
        this->balance=_balance;
        this->blacklevel=_blacklevel;
        this->gamma=_gamma;
        this->gainraw=_gainraw;
        this->sharpness=_sharpness;
        init();
     }

     std::vector<std::string> MyDahuaCam::getDevices(){
        TVector<ICameraPtr> vCameraPtrList;
        bool isDiscoverySuccess = CSystem::getInstance().discovery(vCameraPtrList);
        isDiscoverySuccess=checkCamera(vCameraPtrList);
        std::vector<std::string> devices;
        if(isDiscoverySuccess){
            for(auto it=vCameraPtrList.begin();it!=vCameraPtrList.end();++it){
                const char* serialnumber = (*it)->getSerialNumber();
                std::string v_d(serialnumber);
                devices.emplace_back(v_d);
            }
        }
        return devices;
     }

    void MyDahuaCam::init(){
        inited=false;
        TVector<ICameraPtr> vCameraPtrList;
        bool isDiscoverySuccess = systemObj.discovery(vCameraPtrList);
        isDiscoverySuccess=checkCamera(vCameraPtrList);
        if (!isDiscoverySuccess)
        {
            printf("no device found.\n");
            // exit(1) ;
            return;
        }
        displayDeviceInfo(vCameraPtrList);
        if(mcam_index==-1){
            std::cerr<<"can not find a camera with serial number:"<<mSerialNumber<<"!"<<std::endl;
            return;
        }
        cameraSptr = vCameraPtrList[mcam_index];
        if( ICamera::typeGige == cameraSptr->getType())
        {
            if(autoSetCameraIP() != 0)
            {
                printf("set camera Ip failed.\n");
            }
        }


        if (!cameraSptr->connect())
        {
            printf("connect cameral failed.\n");
            return;
        }

        setResolution(this->width,this->height);
        setBalanceRatio(this->balance[0],this->balance[1],this->balance[2]);
        setBrightness(this->brightness);
        setDigitalShift(this->digitalshift);
        setExposureTime(this->exposure);
        setBlackLevel(this->blacklevel);
        setGamma(this->gamma);
        setGainRaw(this->gainraw);
        setSharpness(this->sharpness);

        setGrabMode( true);

       streamPtr = systemObj.createStreamSource(cameraSptr);
        if (NULL == streamPtr)
        {
            printf("create stream obj  fail.\r\n");
            return ;
        }
        bool isStartGrabbingSuccess = streamPtr->startGrabbing();
        if (!isStartGrabbingSuccess)
        {
            printf("StartGrabbing  fail.\n");
        } else{
            printf("StartGrabbing  succeed.\n");
        }
        inited=true;
    }


    /**
* Copyright (C) 2019, HITCRT_VISION, all rights reserved.
* @brief 抓图函数
* @param cv::Mat& 相机得到的图像
* @return bool抓图是否成功
* @author 黎林
* -phone:18846140245;qq:1430244438
*/
    bool MyDahuaCam::grab(){
        if(!inited){
            init();
            sleep(1);
            return false;
        }
        CFrame cframe;
        bool isSuccess =streamPtr->getFrame(cframe, 300);
        if(!isSuccess)
        {
            printf("Grabbing  fail.\n");
            faildnum++;
            if(faildnum>5){
                faildnum=0;
                close();
            }
            return false;
        }

        if (!cframe.valid())
        {
            printf("frame is invalid!\n");
            faildnum++;
            if(faildnum>5){
                faildnum=0;
                close();
            }
            return false;
        }
           cv::Mat temp= cv::Mat(cframe.getImageHeight(), cframe.getImageWidth(), CV_8UC1,
                           (uint8_t *) cframe.getImage());
        cv::Mat frame_temp;
        frame_temp.create(temp.rows,temp.cols,CV_8UC3);
        cv::cvtColor(temp,frame_temp,cv::COLOR_BayerBG2BGR);
        {
            boost::unique_lock<boost::shared_mutex> lock(image_mutex);
            mframe=std::make_shared<cv::Mat>(frame_temp);
        }
        faildnum=0;
        return true;
     }
    std::shared_ptr<cv::Mat> MyDahuaCam::getImage(bool destroy){
        if(destroy){
            boost::unique_lock<boost::shared_mutex> lock(image_mutex);
            auto temp=mframe;
            mframe.reset();
            return temp;
        } else{
            boost::shared_lock<boost::shared_mutex> lock(image_mutex);
            return mframe;
        }
    }
    void MyDahuaCam::close(){
        if(!inited){
            return;
        }
        inited=false;
        streamPtr->stopGrabbing();
        // modifyCamralExposureTime(systemObj);
        if (!cameraSptr->disConnect())
        {
            printf("disConnect camera failed\n");
        }
        printf("disConnect successfully thread ID :%d\n", CThread::getCurrentThreadID());
    }


}